package com.bw.gmall.realtime.Day0917;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.utils.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternFlatSelectFunction;
import org.apache.flink.cep.PatternFlatTimeoutFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.util.List;
import java.util.Map;
        /*
数据流行：
   数据源在哪里
   1.日志服务器
   2.flume采集到kafka
   3.分流 数据量大  业务复杂 新老用户修复
   4.flink加载 page 页面数据
   5.跳出   只有一个页面的会话    超时
   6.我们定义CEP
   7.把规则应用到流上
   8.取出符合规则的流和超时流
   9.存入到kafka
   落盘跳出数据
* */

public class DwdUserJump {
    public static void main(String[] args) throws Exception {
        /*
        1.从 Kafka 读取页面日志。
        2.解析 JSON 数据。
        3.提取时间戳并设置水位线（Watermark）。
        4.按用户 ID (mid) 分组。
        5.使用 CEP 定义模式：先匹配一个 last_page_id == null 的事件（即首次进入或无上一页），紧接着在 10 秒内再出现一个 last_page_id == null 的事件。
        6.输出匹配成功的事件（主流）和超时未匹配的事件（超时流）。
        7.合并两个流后写入 Kafka。
        **/
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // TODO: 2025/9/13 2.状态后端设置
//        env.setStateBackend(new HashMapStateBackend());
//        env.getCheckpointConfig().setCheckpointStorage("file:///checkpoint-dir");

        // TODO: 2025/9/13 3.从kafka dwd_traffic_page_log 主题读取日志数据，封装为流
        String topic = "dwd_traffic_page_log";
        String groupId = "dwd_traffic_user_jump_detail";

        FlinkKafkaConsumer<String> kafkaConsumer =
                MyKafkaUtil.getFlinkKafkaConsumer(topic, groupId);
        DataStreamSource<String> pageLog = env.addSource(kafkaConsumer);

        SingleOutputStreamOperator<JSONObject> mappedStream = pageLog.flatMap(
                new FlatMapFunction<String, JSONObject>() {
                    @Override
                    public void flatMap(String s, Collector<JSONObject> collector) throws Exception {
                        try {
                            JSONObject jsonObject = JSON.parseObject(s);
                            collector.collect(jsonObject);
                        } catch (Exception e) {
                            System.out.println("脏数据" + s);
                        }
                    }
                }
        );

        // TODO: 2025/9/13 设置水位线 用于用户跳出
        SingleOutputStreamOperator<JSONObject> withWatermarkStream =
                mappedStream.assignTimestampsAndWatermarks(WatermarkStrategy.<JSONObject>forMonotonousTimestamps()
                .withTimestampAssigner(new SerializableTimestampAssigner<JSONObject>() {
                    @Override
                    public long extractTimestamp(JSONObject jsonObject, long l) {
                        return jsonObject.getLong("ts");
                    }
                }));

        // TODO: 2025/9/13 按照mid 分组
        KeyedStream<JSONObject, String> keyedStream = withWatermarkStream.keyBy(r -> r.getJSONObject("common")
                .getString("mid"));
        //keyedStream.print("原始数据(水位线和分组)===========>");

        // TODO: 2025/9/13 定义CEP规则
        Pattern<JSONObject, JSONObject> pattern = Pattern.<JSONObject>begin("first")
                .where(new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) throws Exception {
                        String lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        return lastPageId == null;
                    }
                })
                .next("second")
                .where(new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) throws Exception {
                        String lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        return lastPageId == null;
                    }
                }).within(Time.seconds(10));

        // TODO 8. 把 Pattern 应用到流上
        PatternStream<JSONObject> patternStream = CEP.pattern(keyedStream, pattern);

        // TODO: 2025/9/13 9. 提取匹配上的事件以及超时事件
        OutputTag<JSONObject> timeoutTag = new OutputTag<JSONObject>("timeoutTag") {};


        
        SingleOutputStreamOperator<JSONObject> flatSelectStream = patternStream.flatSelect(
                timeoutTag,
                new PatternFlatTimeoutFunction<JSONObject, JSONObject>() {
                    @Override
                    public void timeout(Map<String, List<JSONObject>> map, long l, Collector<JSONObject> collector) throws Exception {
                        // TODO: 2025/9/13 超时时也输出第一个事件（first）
                        JSONObject element = map.get("first").get(0);
                        //超时流
                        collector.collect(element);
                    }
                },
                new PatternFlatSelectFunction<JSONObject, JSONObject>() {
                    @Override
                    public void flatSelect(Map<String, List<JSONObject>> map, Collector<JSONObject> collector) throws Exception {
                        // TODO: 2025/9/13 当模式匹配成功时，输出第一个事件（first）。 也就是说，无论是否匹配，都会把第一个符合条件的事件输出一次。
                        JSONObject element = map.get("first").get(0);
                        collector.collect(element);
                    }
                }
        );

        flatSelectStream.print("主流===========>");
        DataStream<JSONObject> timeOutDStream = flatSelectStream.getSideOutput(timeoutTag);
        timeOutDStream.print("超时流===========>");

        // TODO: 2025/9/13 合并两个流写出到kafka
        DataStream<JSONObject> unionDStream = flatSelectStream.union(timeOutDStream);
        unionDStream.print("=====================>");
        String targetTopic = "dwd_traffic_user_jump_detail";
        FlinkKafkaProducer<String> kafkaProducer = MyKafkaUtil.getFlinkKafkaProducer(targetTopic);
        unionDStream.map(JSONAware::toJSONString).addSink(kafkaProducer);

        env.execute();


    }
}
